// pages/online-memorial/online-memorial.js
Page({
  data: {
    donorId: '',
    donorName: '',
    donorInfo: {},
    candleLit: false,
    flowersOffered: false,
    incenseBurned: false,
    wineOffered: false,
    hasBowed: false,
    candleCount: 0,
    flowerCount: 0,
    incenseCount: 0,
    wineCount: 0,
    bowCount: 0,
    messageCount: 0,
    totalCandles: 0,
    totalFlowers: 0,
    totalIncense: 0,
    totalWine: 0,
    totalBows: 0,
    totalMessages: 0,
    showMessageModal: false,
    newMessage: '',
    memorialRecords: []
  },

  onLoad: function (options) {
    const { donorId, donorName } = options;
    this.setData({
      donorId: donorId,
      donorName: donorName
    });
    
    // 设置页面标题
    wx.setNavigationBarTitle({
      title: `${donorName} - 线上祭奠`
    });
    
    // 加载捐献者信息
    this.loadDonorInfo(donorId);
    // 加载祭奠数据
    this.loadMemorialData(donorId);
  },

  // 加载捐献者信息
  loadDonorInfo: function (id) {
    // 模拟从服务器获取捐献者信息
    const donorData = this.getMockDonorData(id);
    this.setData({
      donorInfo: donorData
    });
  },

  // 加载祭奠数据
  loadMemorialData: function (id) {
    // 模拟从服务器获取祭奠数据
    const memorialData = {
      totalCandles: 156,
      totalFlowers: 89,
      totalIncense: 67,
      totalWine: 45,
      totalBows: 123,
      totalMessages: 234,
      records: [
        {
          id: 1,
          author: '张小明',
          time: '2025.08.15 14:30',
          action: '为捐献者点了一支蜡烛',
          avatar: '/images/avatar-1.jpg'
        },
        {
          id: 2,
          author: '李志愿者',
          time: '2025.08.15 13:45',
          action: '为捐献者献了一朵鲜花',
          avatar: '/images/avatar-2.jpg'
        },
        {
          id: 3,
          author: '王医生',
          time: '2025.08.15 12:20',
          action: '为捐献者上了一支香',
          avatar: '/images/avatar-3.jpg'
        }
      ]
    };
    
    this.setData({
      totalCandles: memorialData.totalCandles,
      totalFlowers: memorialData.totalFlowers,
      totalIncense: memorialData.totalIncense,
      totalWine: memorialData.totalWine,
      totalBows: memorialData.totalBows,
      totalMessages: memorialData.totalMessages,
      memorialRecords: memorialData.records
    });
  },

  // 获取模拟捐献者数据
  getMockDonorData: function (id) {
    const donors = {
      '1': {
        id: 1,
        name: '张明华',
        birthYear: '1955',
        deathYear: '2022',
        type: '器官捐献者',
        avatar: '/images/donor-1.jpg'
      },
      '2': {
        id: 2,
        name: '李秀英',
        birthYear: '1962',
        deathYear: '2023',
        type: '遗体捐献者',
        avatar: '/images/donor-2.jpg'
      },
      '3': {
        id: 3,
        name: '王建国',
        birthYear: '1948',
        deathYear: '2023',
        type: '器官捐献者',
        avatar: '/images/donor-3.jpg'
      }
    };
    
    return donors[id] || donors['1'];
  },

  // 点烛
  lightCandle: function () {
    if (this.data.candleLit) {
      wx.showToast({
        title: '您已经点过蜡烛了',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '点烛中...'
    });

    setTimeout(() => {
      wx.hideLoading();
      
      this.setData({
        candleLit: true,
        candleCount: this.data.candleCount + 1,
        totalCandles: this.data.totalCandles + 1
      });

      // 添加祭奠记录
      const newRecord = {
        id: Date.now(),
        author: '我',
        time: this.formatTime(new Date()),
        action: '为捐献者点了一支蜡烛',
        avatar: '/images/avatar-default.jpg'
      };

      this.setData({
        memorialRecords: [newRecord, ...this.data.memorialRecords]
      });

      wx.showToast({
        title: '已为捐献者点烛',
        icon: 'success',
        duration: 2000
      });
    }, 1000);
  },

  // 献花
  offerFlowers: function () {
    if (this.data.flowersOffered) {
      wx.showToast({
        title: '您已经献过花了',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '献花中...'
    });

    setTimeout(() => {
      wx.hideLoading();
      
      this.setData({
        flowersOffered: true,
        flowerCount: this.data.flowerCount + 1,
        totalFlowers: this.data.totalFlowers + 1
      });

      // 添加祭奠记录
      const newRecord = {
        id: Date.now(),
        author: '我',
        time: this.formatTime(new Date()),
        action: '为捐献者献了一朵鲜花',
        avatar: '/images/avatar-default.jpg'
      };

      this.setData({
        memorialRecords: [newRecord, ...this.data.memorialRecords]
      });

      wx.showToast({
        title: '已为捐献者献花',
        icon: 'success',
        duration: 2000
      });
    }, 1000);
  },

  // 上香
  burnIncense: function () {
    if (this.data.incenseBurned) {
      wx.showToast({
        title: '您已经上过香了',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '上香中...'
    });

    setTimeout(() => {
      wx.hideLoading();
      
      this.setData({
        incenseBurned: true,
        incenseCount: this.data.incenseCount + 1,
        totalIncense: this.data.totalIncense + 1
      });

      // 添加祭奠记录
      const newRecord = {
        id: Date.now(),
        author: '我',
        time: this.formatTime(new Date()),
        action: '为捐献者上了一支香',
        avatar: '/images/avatar-default.jpg'
      };

      this.setData({
        memorialRecords: [newRecord, ...this.data.memorialRecords]
      });

      wx.showToast({
        title: '已为捐献者上香',
        icon: 'success',
        duration: 2000
      });
    }, 1000);
  },

  // 敬酒
  offerWine: function () {
    if (this.data.wineOffered) {
      wx.showToast({
        title: '您已经敬过酒了',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '敬酒中...'
    });

    setTimeout(() => {
      wx.hideLoading();
      
      this.setData({
        wineOffered: true,
        wineCount: this.data.wineCount + 1,
        totalWine: this.data.totalWine + 1
      });

      // 添加祭奠记录
      const newRecord = {
        id: Date.now(),
        author: '我',
        time: this.formatTime(new Date()),
        action: '为捐献者敬了一杯酒',
        avatar: '/images/avatar-default.jpg'
      };

      this.setData({
        memorialRecords: [newRecord, ...this.data.memorialRecords]
      });

      wx.showToast({
        title: '已为捐献者敬酒',
        icon: 'success',
        duration: 2000
      });
    }, 1000);
  },

  // 鞠躬
  bow: function () {
    if (this.data.hasBowed) {
      wx.showToast({
        title: '您已经鞠过躬了',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '鞠躬中...'
    });

    setTimeout(() => {
      wx.hideLoading();
      
      this.setData({
        hasBowed: true,
        bowCount: this.data.bowCount + 1,
        totalBows: this.data.totalBows + 1
      });

      // 添加祭奠记录
      const newRecord = {
        id: Date.now(),
        author: '我',
        time: this.formatTime(new Date()),
        action: '向捐献者深深鞠躬',
        avatar: '/images/avatar-default.jpg'
      };

      this.setData({
        memorialRecords: [newRecord, ...this.data.memorialRecords]
      });

      wx.showToast({
        title: '已向捐献者鞠躬',
        icon: 'success',
        duration: 2000
      });
    }, 1000);
  },

  // 写留言
  writeMessage: function () {
    this.setData({
      showMessageModal: true
    });
  },

  // 关闭留言弹窗
  closeMessageModal: function () {
    this.setData({
      showMessageModal: false,
      newMessage: ''
    });
  },

  // 留言输入
  onMessageInput: function (e) {
    this.setData({
      newMessage: e.detail.value
    });
  },

  // 提交留言
  submitMessage: function () {
    if (!this.data.newMessage.trim()) {
      wx.showToast({
        title: '请输入留言内容',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '提交中...'
    });

    setTimeout(() => {
      wx.hideLoading();
      
      this.setData({
        messageCount: this.data.messageCount + 1,
        totalMessages: this.data.totalMessages + 1
      });

      // 添加祭奠记录
      const newRecord = {
        id: Date.now(),
        author: '我',
        time: this.formatTime(new Date()),
        action: `留言：${this.data.newMessage}`,
        avatar: '/images/avatar-default.jpg'
      };

      this.setData({
        memorialRecords: [newRecord, ...this.data.memorialRecords],
        showMessageModal: false,
        newMessage: ''
      });

      wx.showToast({
        title: '留言提交成功',
        icon: 'success'
      });
    }, 1000);
  },

  // 格式化时间
  formatTime: function (date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    
    return `${year}.${month}.${day} ${hour}:${minute}`;
  },

  // 分享功能
  onShareAppMessage: function () {
    const { donorInfo } = this.data;
    return {
      title: `${donorInfo.name} - 线上祭奠`,
      path: `/pages/online-memorial/online-memorial?donorId=${this.data.donorId}&donorName=${this.data.donorName}`,
      imageUrl: donorInfo.avatar
    };
  },

  // 分享到朋友圈
  onShareTimeline: function () {
    const { donorInfo } = this.data;
    return {
      title: `${donorInfo.name} - 线上祭奠`,
      imageUrl: donorInfo.avatar
    };
  }
});